package cn.hx.admin.controller;

import cn.hx.admin.entity.ScheduleTask;
import cn.hx.admin.service.ScheduleTaskService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
/**
 * 定时任务(ScheduleTask)表控制层
 *
 * @author makejava
 * @since 2023-12-07 11:32:58
 */
@RestController
@RequestMapping("scheduleTask")
public class ScheduleTaskController {
    /**
     * 服务对象
     */
    @Resource
    private ScheduleTaskService scheduleTaskService;

    /**
     * 分页查询
     *
     * @param scheduleTask 筛选条件
     * @return 查询结果
     */
    @GetMapping
    @PreAuthorize("hasAuthority('system:scheduleTask:list')")
    public ResponseEntity<List<ScheduleTask>> queryByPage(ScheduleTask scheduleTask) {
        System.out.println(scheduleTask);
        return ResponseEntity.ok(this.scheduleTaskService.queryByPage(scheduleTask));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('monitor:task:query')")
    public ResponseEntity<ScheduleTask> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.scheduleTaskService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param scheduleTask 实体
     * @return 新增结果
     */
    @PostMapping
    @PreAuthorize("hasAuthority('monitor:task:add')")
    public ResponseEntity<ScheduleTask> add(@RequestBody ScheduleTask scheduleTask) {
        System.out.println(scheduleTask);
        return ResponseEntity.ok(this.scheduleTaskService.insert(scheduleTask));
    }

    /**
     * 编辑数据
     *
     * @param scheduleTask 实体
     * @return 编辑结果
     */

    @PutMapping
    @PreAuthorize("hasAuthority('monitor:task:edit')")
    public ResponseEntity<ScheduleTask> edit(@RequestBody ScheduleTask scheduleTask) {
        ScheduleTask update = this.scheduleTaskService.update(scheduleTask);
        return ResponseEntity.ok(update);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    @PreAuthorize("hasAuthority('monitor:task:remove')")
    public ResponseEntity<Boolean> deleteById(String id) {
        System.out.println(id);
        return ResponseEntity.ok(this.scheduleTaskService.deleteById(Long.valueOf(id)));
    }

    /**
     * 定时任务状态控制
     * @param id
     * @param status
     * @return
     */
    @PostMapping("task")
    @PreAuthorize("hasAuthority('monitor:task:status')")
    public ResponseEntity<ScheduleTask> changeStatus(String id,String status) {
        return ResponseEntity.ok(this.scheduleTaskService.changeStatus(Long.valueOf(id),status));
    }

}

